package com.nosql.client.api;

import com.nosql.common.protocol.DBResponse;
import com.nosql.db.network.DBCommand;
import com.nosql.db.core.modul.Document;

import java.io.*;
import java.net.Socket;
import java.util.Map;

public class NoSQLClient implements AutoCloseable {
    private Socket socket;
    private ObjectOutputStream out;
    private ObjectInputStream in;






    public void connect(String host, int port) throws IOException {
        socket = new Socket(host, port);
        out = new ObjectOutputStream(socket.getOutputStream());
        in = new ObjectInputStream(socket.getInputStream());
    }

    public String put(String collection, Map<String, Object> data) throws IOException {
        DBCommand.putCommand command = new DBCommand.putCommand(collection, data, null);
        DBResponse response = (DBResponse) sendCommand(command);

        if (response.getStatus() == DBResponse.Status.SUCCESS) {
            return (String) response.getData();// 返回生成的ID
        } else {
            throw new IOException("写入失败: " + response.getError());
        }
    }

    public Document get(String collection, String id) throws IOException {
        DBCommand.GetCommand command = new DBCommand.GetCommand(collection, id);
        DBResponse response = (DBResponse) sendCommand(command);

        if (response.getStatus() == DBResponse.Status.SUCCESS) {
            return (Document) response.getData();
        } else if (response.getStatus() == DBResponse.Status.NOT_FOUND) {
            return null;
        } else {
            throw new IOException("查询失败: " + response.getError());
        }
    }

    public void delete(String collection, String id) throws IOException {
        DBCommand.DeleteCommand command = new DBCommand.DeleteCommand(collection, id);
        sendCommand(command);
    }

    public void flush() throws IOException {
        DBCommand.FlushCommand command = new DBCommand.FlushCommand();
        sendCommand(command);
    }


    public void createCollection(String trim) throws IOException {
         DBCommand.CreateCollectionCommand command = new DBCommand.CreateCollectionCommand(trim);
          sendCommand(command);
    }

    private Object sendCommand(DBCommand command) throws IOException {
        out.writeObject(command);
        out.flush();
        try {
            return in.readObject();
        } catch (ClassNotFoundException e) {
            throw new IOException("Invalid response", e);
        }
    }


    @Override
    public void close() {
        try {
            if (out != null) out.close();
            if (in != null) in.close();
            if (socket != null) socket.close();
            System.out.println("客户端连接已关闭");
        } catch (IOException e) {
            System.err.println("关闭客户端连接时发生错误: " + e.getMessage());
        }
    }

    public DBResponse update(String part, String part1, Map<String, Object> updateData) throws IOException {
        DBCommand.UpdateCommand command = new DBCommand.UpdateCommand(part, part1, updateData);
        DBResponse response = (DBResponse) sendCommand(command);
        if (response.getStatus() == DBResponse.Status.SUCCESS) {
            System.out.println("更新成功");
        } else {
            System.out.println("更新失败: " + response.getError());
        }
        return response;
    }
}